package application2.contorller;

import common.action.SaveProductAction;
import common.domain.Product;
import common.form.ProductForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 返回ProductDetail.jsp页面
 */
public class SaveProductController implements Controller{
    public String handleRequest(HttpServletRequest request, HttpServletResponse response) {

        // 1、获取前端传来的数据
        ProductForm productForm = new ProductForm();
        productForm.setName(request.getParameter("name"));
        productForm.setDescription(request.getParameter("description"));
        productForm.setPrice(request.getParameter("price"));

        // 2、将Form Object 转化为Domain Object
        Product product1 = new Product();
        product1.setName(productForm.getName());
        product1.setDescription(productForm.getDescription());
        try {
            product1.setPrice(Float.parseFloat(productForm.getPrice()));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        // 3、业务操作
        SaveProductAction saveProductAction = new SaveProductAction();
        saveProductAction.save(product1);

        // 4、存储HttpSession中
        request.setAttribute("product", product1);

        // 5、页面跳转
        return "/WEB-INF/application2/jsp/ProductDetail2.jsp";
    }
}
